(function (vc) {
    vc.extends({
        data: {
            addPrivilegeInfo: {
                _currentPgId: '',
                _pName: '',
                name: '',
                description: '',
                errorInfo: '',
                _noAddPrivilege: [],
                selectPrivileges: []
            }
        },
        watch: { // 监视双向绑定的数据数组
            addPrivilegeInfo: {
                handler() { // 数据数组有变化将触发此函数
                    if ($that.addPrivilegeInfo.selectPrivileges.length == $that.addPrivilegeInfo._noAddPrivilege.length) {
                        document.querySelector('#quan').checked = true;
                    } else {
                        document.querySelector('#quan').checked = false;
                    }
                },
                deep: true // 深度监视
            }
        },
        _initMethod: function () {
        },
        _initEvent: function () {
            $that.$on('addPrivilege_openPrivilegeModel', function (_params) {
                $('#addPrivilegeModel').modal('show');
                $that.addPrivilegeInfo._currentPgId = _params.pgId;
                //查询没有添加的权限
                $that.listNoAddPrivilege();
            });
        },
        methods: {
            listNoAddPrivilege: function () {
                $that.addPrivilegeInfo._noAddPrivilege = [];
                var param = {
                    params: {
                        pgId: $that.addPrivilegeInfo._currentPgId,
                        pName: $that.addPrivilegeInfo._pName
                    }
                }
                vc.http.get(
                    'addPrivilege',
                    'listNoAddPrivilege',
                    param,
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status == 200) {
                            $that.addPrivilegeInfo._noAddPrivilege = JSON.parse(json);
                            return;
                        }
                        $that.addPrivilegeInfo.errorInfo = json;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        $that.addPrivilegeInfo.errorInfo = errInfo;
                    });
            },
            addPrivilegeToPrivilegeGroup: function () {
                $that.addPrivilegeInfo.errorInfo = "";
                var _selectPrivileges = $that.addPrivilegeInfo.selectPrivileges;
                if (_selectPrivileges.length < 1) {
                    vc.toast("请先选择权限");
                    return;
                }
                var _pIds = [];
                for (var selectIndex = 0; selectIndex < _selectPrivileges.length; selectIndex++) {
                    var _pId = {
                        pId: _selectPrivileges[selectIndex]
                    };
                    _pIds.push(_pId);
                }
                var _objData = {
                    pgId: $that.addPrivilegeInfo._currentPgId,
                    pIds: _pIds
                };
                vc.http.post(
                    'addPrivilege',
                    'addPrivilegeToPrivilegeGroup',
                    JSON.stringify(_objData),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        if (res.status == 200) {
                            //关闭model
                            $that.listNoAddPrivilege();
                            $('#addPrivilegeModel').modal('hide');
                            $that.$emit('privilege_loadPrivilege', $that.addPrivilegeInfo._currentPgId);
                            vc.toast("操作成功");
                            return;
                        }
                        $that.addPrivilegeInfo.errorInfo = json;
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        $that.addPrivilegeInfo.errorInfo = errInfo;
                    });
            },
            checkAll: function (e) {
                var checkObj = document.querySelectorAll('.checkItem'); // 获取所有checkbox项
                if (e.target.checked) { // 判定全选checkbox的勾选状态
                    for (var i = 0; i < checkObj.length; i++) {
                        if (!checkObj[i].checked) { // 将未勾选的checkbox选项push到绑定数组中
                            $that.addPrivilegeInfo.selectPrivileges.push(checkObj[i].value);
                        }
                    }
                } else { // 如果是去掉全选则清空checkbox选项绑定数组
                    $that.addPrivilegeInfo.selectPrivileges = [];
                }
            }
        }
    });
})(window.vc);